package com.pingan.controller;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pingan.stat.reposys.general.service.GeneralStatService;
import com.pingan.stat.service.StatService;

@Controller
public class GeneralStatController implements InitializingBean {

	@Resource
	private ApplicationContext applicationContext;

	@Resource
	private GeneralStatService generalStatService;

	@RequestMapping("/generalstat")
	public void getValue(final String date, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		Date d = null;
		if (StringUtils.isBlank(date) || StringUtils.isEmpty(date)) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_YEAR, -1);
			d = cal.getTime();
		} else {
			d = DateUtils.parseDate(date, new String[] { "yyyyMMdd" });
		}

		generalStatService.dailyStat(d);
	}

	@RequestMapping("/statByName")
	public void statByName(final String date, final String name,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		Date d = null;
		if (StringUtils.isBlank(date) || StringUtils.isEmpty(date)) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_YEAR, -1);
			d = cal.getTime();
		} else {
			d = DateUtils.parseDate(date, new String[] { "yyyyMMdd" });
		}

		StatService stat = (StatService) applicationContext.getBean(name);

		if (stat != null) {
			stat.dailyStat(d);
		} else {
			System.out.println("------ " + name + " is null");
		}

	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}

}
